<template>
  <el-dialog width="60%" :title="$t('m.initInventory.add')" :visible.sync="dialogVisible" :before-close="close" :close-on-click-modal="false">
    <el-form size="small" ref="form" :model="form" label-width="120px">
      <el-row>
        <el-steps :active="active" finish-status="success" align-center>
          <template v-for="(item,index) in [...stepOptions,{label:$t('m.inventory.table')},]">
            <el-step :title="item.label" :key="index"></el-step>
          </template>
        </el-steps>
      </el-row>

      <template v-for="(item,index) in stepOptions">
        <el-row class="select-row" :key="index" v-show="active===index">
          <el-col :span="12" :offset="5">
            <el-form-item :label="item.label">
              <el-select v-model="form[item.key]">
                <el-option
                  v-for="(item,index) in options[index]"
                  :key="index"
                  :label="`${item.label}${item.remark?item.remark:''}`"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
      <el-row v-show="active===stepOptions.length">
        <el-col :span="12" :offset="6">
          <i class="el-icon-loading"></i>
          {{$t('m.inventory.making')}}
        </el-col>
      </el-row>
    </el-form>
    <el-row v-show="active!==2">
      <el-button @click="backStep">{{active?$t('m.inventory.last'):$t('m.cancel')}}</el-button>
      <el-button type="primary" @click="nextStep">{{active?$t('m.inventory.finish'):$t('m.inventory.next')}}</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
export default {
  name:'inventory-step-popup',
  props:['dialogVisible','options'],
  data(){
    return {
      active:0,
      form:{
        stocktype:'', // 盘点类型
        stockway:'' // 盘点方式
      },
      stocktypeOptions:[], // 盘点类型options
      stockwayOptions:[], // 盘点方式options

      loading:false
    }
  },
  mounted(){
    let [stocktypeOptions,stockwayOptions] = this.options
    this.stocktypeOptions = stocktypeOptions
    this.stockwayOptions = stockwayOptions
  },
  computed:{
    stepOptions(){
      return[
        {label:this.$t('m.inventory.type'),key:'stocktype'},
        {label:this.$t('m.inventory.method'),key:'stockway'}
      ]}
  },
  methods:{
    close(){
      this.$emit('close')
    },
    backStep(){
      if(!this.active){
        this.close()
      } else {
        --this.active
      }
    },
    nextStep(){
      if(!this.form[this.stepOptions[this.active].key]){
        return this.callbackMessage(`${this.$t('m.choose')+this.stepOptions[this.active].label}`)
      }
      if(this.active===1){ // 最终step
        setTimeout(()=>{
          this.close()
          this.$emit('submit',this.form)
        },1500)
      }
      this.active++
    }
  }
}
</script>
<style lang="scss" scoped>
.select-row{margin-top: 18px;}
</style>